// pages/my/order/index.js
const app = getApp()
Page({
  data: {
    active: 0,
    screenWidth: 0,
    imgwidth: 0,
    imgheight: 0,
    items:[],
    serverHost: app.config.serverHost,
    filterData:[], // 过滤时存储的数据
    off:false,
    offlist:[]
  },
  deleteorder(){
 
  },
  onLoad(){
    let _this = this;
    if (!wx.getStorageSync('token')) {
      wx.showToast({
        title: '请先登录...',
        image: '../../static/icons/err.png',
        duration: 2000,
        success: function (res) {
          wx.navigateTo({
            url: '../loging/index',
          })
        }
      })
      return
    }
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({screenWidth: res.windowWidth})
      }
    })
    const token = {token:wx.getStorageSync('token')}
    new Promise((resolve,reject)=>{
      app.api.getData('web/order', token, (res) => {
        let data = res.data
        data.filter(item=>{
          item.order_time = app.util.formatTime(item.order_time)
        })
        _this.setData({
          items: res.data.reverse()
        })
        resolve(res.data)
      })
    })
    .then(data=>{
      _this.setData({
        filterData:data
      })
    })
  },
  imageLoad: function (e) {
    var screenW = this.data.screenWidth;
    var viewWidth = 150 * screenW / 750;
    var viewHeight = viewWidth;
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },
  // 数据过滤
  onchange:function(a){
    var item = this.data.filterData;
    var i = item.filter(e=>{
      return a.detail.title == "全部" ? item : e.status == a.detail.title
    })
    this.setData({
      items: i
    })
  },
  // 订单详情页面
  orderTracking:function(e){
var _this = this;
var off = false;
    if (e.currentTarget.dataset.haha.status=='待支付'){
      console.log(e.currentTarget.dataset.haha)
      var date = e.currentTarget.dataset.haha
      var id = date.goods_list
      console.log(id)
      new Promise((resolve, reject) => {
      app.api.post('web/goodslistnumber', id, (res) => {
        console.log(res.data)
        res.data.forEach(function (item, index) {
          console.log(item);
          console.log(item.number)
          var jsdata = (id)
          console.log(index)
          console.log(jsdata[index])
          if (item.number - jsdata[index].value < 0) {
            off = true
            console.log(off)
            _this.data.offlist.push(item.name)
            console.log(_this.data.offlist)
          } else {
    console.log("ssssssssssssssssss")
          }
        })
        console.log(off)
        resolve(off)
      })
      }).then(data => {
        if (!data) {
          var datas = {
            status: date.status,
            body: "小象精选",
            openid: wx.getStorageSync('openid'),
            paySigns: date.out_trade_no,
            cash_fee: date.cash_fee,
            beizhu: date.beizhu,
            way: date.way,
            token: wx.getStorageSync('token')
          }
          console.log(datas)


          app.api.getData('/pays', datas, (res) => {
            console.log(res)
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.sign,

              'success': function (res) {
                console.log("success", res)
                app.api.getData('/order', datas, (res) => {
                })
                wx.redirectTo({
                  url: '/pages/my/order/index',
                })

              },
              'fail': function (res) {
                console.log("fail", res)
                // wx.redirectTo({
                //   url: '/pages/my/order/index',
                // })
              },
            })
          })
        }else{
          wx.showModal({
            title: '缺货提示',
            content: _this.data.offlist + "缺货" + "\n" + "请重新下单",
            success: function (res) {
              if (res.confirm) {
           
          //删除订单
                var datas = {
                  date: date,
                  token: wx.getStorageSync('token')
                }
                console.log(datas)
                app.api.deleteData('web/order', datas, (res) => {
                  console.log(res)
                  res.data.filter(item => {
                    item.order_time = app.util.formatTime(item.order_time)
                  })
                  _this.setData({
                    items: res.data,
                    filterData: res.data.reverse()
                  })
                  console.log(_this.data.items)
                })
                wx.showToast({
                  title: '删除成功',
                  duration: 1000,
                  mask: true    //是否有透明蒙层，默认为false 
                  //如果有透明蒙层，弹窗的期间不能点击文档内容 
                })
                  _this.setData({
                    offlist: []
                  })
              } else {
                console.log('点击取消回调')
                _this.setData({
                  offlist: []
                })
              }
            }
          }) 

          
        }

      })


      

    // wx.navigateTo({
    //   url: "/pages/my/order/orderTracking/index"
    // })
    }else{
      wx.showToast({
        title: '下单成功',
        duration: 1000,
        mask: true    //是否有透明蒙层，默认为false 
        //如果有透明蒙层，弹窗的期间不能点击文档内容 
      })
    }
  
  },
  calcelOrder: function (e){
    console.log(e.currentTarget.dataset.haha)
    var _this = this;
    var date = e.currentTarget.dataset.haha
    if (e.currentTarget.dataset.haha.status == '待支付') {
      wx.showModal({
        title: '提示',
        content: '确定要删除此订单吗？',
        success: function (res) {
          if (res.confirm) {
            console.log('点击确定了');
            var datas = {
              date: date,
              token: wx.getStorageSync('token')
            }
            console.log(datas)
            app.api.deleteData('web/order', datas, (res) => {
              console.log(res)
              res.data.filter(item => {
                item.order_time = app.util.formatTime(item.order_time)
              })
              _this.setData({
                items: res.data,
                filterData: res.data.reverse()
              })
              console.log(_this.data.items)
            })
            wx.showToast({
              title: '删除成功',
              duration: 1000,
              mask: true    //是否有透明蒙层，默认为false 
              //如果有透明蒙层，弹窗的期间不能点击文档内容 
            })
          } else if (res.cancel) {
            console.log('点击取消了');
            return false;
          }
        
        }
      })
    }
  }
})